package com.my.service.Impl;

import com.my.entity.*;
import com.my.mapper.ClazzMapper;
import com.my.mapper.StudentMapper;
import com.my.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClazzMapper clazzMapper;

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Override
    public PageBean select(StudentQueryParam param) {
        // 1. 获取 页码及每页记录数
        Integer pageSize = param.getPageSize();
        param.setPage((param.getPage() - 1) * pageSize);
        Integer page = param.getPage();

        // 1.2 添加查询条件到 student对象

        // 1.3 获取总页数
        Integer count = studentMapper.total();

        // 2. 调用mapper的方法 获得Student列表
        // 遍历数据获取全部查询的学生对象
        List<Student> studentList = studentMapper.select(param);

        PageBean pageBean = new PageBean();
        pageBean.setTotal(count);
        pageBean.setRows(studentList);
        return pageBean;
    }

    /**
     * 删除学员数据
     * @param ids
     */
    @Override
    public void delete(Integer[] ids) {
        // 调用mapper
        studentMapper.delete(ids);
    }

    /**
     * 插入数据
     * @param student
     */
    @Override
    public void insert(Student student) {
        // 补充信息
        student.setUpdateTime(LocalDateTime.now());
        student.setCreateTime(LocalDateTime.now());

        // 调用mapper
        studentMapper.insert(student);

    }

    /**
     * 根据id查询
     * @param id
     */
    @Override
    public Student selectById(Integer id) {
        log.info("serviceId:{}",id);

        // 调用mapper
        Student student = studentMapper.selectById(id);

        return student;
    }

    /**
     * 修改学员信息
     * @param student
     */
    @Override
    public void update(Student student) {
        // 补充student属性
        student.setUpdateTime(LocalDateTime.now());

        // 调用mapper的方法
        studentMapper.update(student);
    }

    /**
     * 违规处理
     * @param id
     * @param score
     */
    @Override
    public void violation(Integer id, Integer score) {
        studentMapper.violation(id, score);
    }


    /**
     * 统计学员学历
     * @return
     */
    @Override
    public List<DegreeCount> getStudentDegreeData() {

        return studentMapper.getStudentDegreeData();
    }


}
